<template>
	<view>
		<form @submit="formSubmit" report-submit='true'>
			<view class='personal-data'>
				<view class="wrapper" v-if="switchUserInfo.length>0">
					<view class="title">{{$t('mangeAccount')}}</view>
					<view class="wrapList">
						<view class="item acea-row row-between-wrapper" :class="item.uid === userInfo.uid ? 'on' : ''"
							v-for="(item,index) in switchUserInfo" :key="index" @click='switchAccounts(item,index)'>
							<view class="picTxt acea-row row-between-wrapper">
								<!-- @click.stop='uploadpic' -->
								<view class="pictrue" v-if='item.uid === userInfo.uid'>
									<image :src='item.avatar' v-if="item.avatar"></image>
									<image src="/static/images/logo.png" v-else></image>
									<!-- <image src='../../../static/images/alter.png' class="alter"></image> -->
								</view>
								<view class="pictrue" v-else>
									<image :src='item.avatar' v-if="item.avatar"></image>
									<image src="/static/images/logo.png" v-else></image>
								</view>

								<view class="text">
									<view class="name line1">{{ item.nickname }}</view>
									<!-- <view class="phone" v-if="item.phone && item.user_type !='h5'">绑定手機號：{{ item.phone }}</view>
									<view class="phone" v-else-if="item.phone && item.user_type =='h5'">账號：{{ item.phone }}</view>
									<view class="phone" v-else>暫未绑定手機號</view> -->
								</view>
							</view>
							<view class="currentBnt acea-row row-center-wrapper font-color"
								v-if='item.uid === userInfo.uid'>
								{{$t('curAccount')}}
							</view>
							<view class="bnt font-color acea-row row-center-wrapper" v-else>
								{{$t('useAccount')}}
							</view>
						</view>
					</view>
				</view>
				<view class='list'>
					<view class='item acea-row row-between-wrapper'>
						<view>{{$t('nick')}}</view>
						<view class='input' @click="editUsername()"><input type='text' name='nickname'
								:value='userInfo.nickname' disabled></input></view>
					</view>
					<view class='item acea-row row-between-wrapper'>
						<view>{{$t('phone')}}</view>
						<navigator url="/pages/users/user_phone/index" hover-class="none" class="input"
							v-if="!userInfo.phone">
							{{$t('phone')}}<text class="iconfont icon-xiangyou"></text>
						</navigator>
						<navigator hover-class="none" class='input acea-row row-between-wrapper' v-else>
							<input type='text' disabled='true' name='phone' :value='userInfo.phone' class='id'></input>
							<!-- <text class='iconfont icon-you'></text> -->
						</navigator>
					</view>
					<view class='item acea-row row-between-wrapper' @click="changePwd">
						<view>{{$t('password')}}</view>
						<view class='input acea-row row-between-wrapper'>
							<input type='text' :placeholder="$t('editPass')" disabled='true' class='id'></input>
							<text class='iconfont icon-you'></text>
						</view>
					</view>
					<view class='item acea-row row-between-wrapper'>
						<view>{{$t('ID')}}</view>
						<view class='input acea-row row-between-wrapper'>
							<input type='text' :value='userInfo.uid' disabled='true' class='id'></input>
							<text class='iconfont icon-suozi'></text>
						</view>
					</view>
					<view class='item acea-row row-between-wrapper'>
						<view>{{$t('language')}}</view>
						<picker class='input acea-row row-between-wrapper' @change="bindBzChange" :value="bzIndex"
							:range-key="'DEPARTNAME'" :range="bzArray">
							<input type='text' :placeholder="bzArray[bzIndex].DEPARTNAME" disabled='true'
								class='id'></input>
						</picker>
					</view>
					<view class="item acea-row row-between-wrapper" @click="outLogin2">清除缓存</view>
					<!-- 注销用户（假） -->
					<!-- #ifdef MP -->
					<!-- <view class='item acea-row row-between-wrapper'>
						<view>{{$t('setPress')}}</view>
						<view class="input" @click="Setting">
							{{$t('clickMan')}}<text class="iconfont icon-xiangyou"></text>
						</view>
					</view> -->
					<!-- #endif -->
					<!-- <view class="item acea-row row-between-wrapper" v-if="userInfo.phone && userInfo.user_type == 'h5'">
						<view>密碼</view>
						<navigator url="/pages/users/user_pwd_edit/index" hover-class="none" class="input">
							点击修改密碼<text class="iconfont icon-xiangyou"></text>
						</navigator>
					</view> -->
				</view>
				<view class="logOut cart-color acea-row row-center-wrapper" @click="outLogin">{{$t('out')}}</view>
				
				<!-- 	<button class='modifyBnt bg-color' formType="submit">保存修改</button> -->
				<!-- #ifdef H5 -->
				<!-- <view class="logOut cart-color acea-row row-center-wrapper" @click="outLogin" v-if="!this.$wechat.isWeixin()">{{$t('out')}}</view> -->
				<!-- #endif -->
				<!-- #ifdef APP-PLUS -->
<!-- 				<view class="logOut cart-color acea-row row-center-wrapper" @click="outLogin">{{$t('out')}}</view> -->
				<!-- #endif -->
			</view>
		</form>
		<!-- #ifdef MP -->
		<!-- <authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize> -->
		<!-- #endif -->
	</view>
</template>

<script>
	// +----------------------------------------------------------------------
	// | CRMEB [ CRMEB赋能开发者，助力企業发展 ]
	// +----------------------------------------------------------------------
	// | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
	// +----------------------------------------------------------------------
	// | Licensed CRMEB並不是自由软件，未经许可不能去掉CRMEB相關版权
	// +----------------------------------------------------------------------
	// | Author: CRMEB Team <admin@crmeb.com>
	// +----------------------------------------------------------------------
	import {
		getUserInfo,
		userEdit,
		getLogout,
		userAcc
	} from '@/api/user.js';
	import {
		switchH5Login
	} from '@/api/api.js';
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from "vuex";
	import dayjs from "@/plugin/dayjs/dayjs.min.js";
	// #ifdef MP
	import authorize from '@/components/Authorize';
	// #endif
	export default {
		components: {
			// #ifdef MP
			authorize
			// #endif
		},
		data() {
			return {
				userInfo: {},
				isLogin:false,
				loginType: 'h5', //app.globalData.loginType
				userIndex: 0,
				switchUserInfo: [],
				isAuto: false, //没有授权的不會自动授权
				isShowAuth: false, //是否隐藏授权
				// 初始化一个默认空值，防止报错
				bzArray: [{
					DEPARTNAME: this.$t('index.lang.en')
				}, {
					DEPARTNAME: this.$t('index.lang.zh-Hant')
				}, {
					DEPARTNAME: this.$t('index.lang.zh-Hans')
				}, {
					DEPARTNAME: this.$t('index.lang.ja')
				}, {
					DEPARTNAME: this.$t('index.lang.th')
				}, {
					DEPARTNAME: this.$t('index.lang.vi')
				}, {
					DEPARTNAME: this.$t('index.lang.id')
				}, {
					DEPARTNAME: this.$t('index.lang.hi')
				}, {
					DEPARTNAME: this.$t('index.lang.tr')
				}, {
					DEPARTNAME: this.$t('index.lang.ar')
				}, {
					DEPARTNAME: this.$t('index.lang.ko')
				}],
				//,{DEPARTNAME:this.$t('index.lang.vi')},{DEPARTNAME:this.$t('index.lang.id')},{DEPARTNAME:this.$t('index.lang.hi')},{DEPARTNAME:this.$t('index.lang.tr')},{DEPARTNAME:this.$t('index.lang.ar')}
				// 当前选中的下标，触发change事件后需要跟着变
				bzIndex: 0,
			};
		},
		// computed: mapGetters(['isLogin']),
		onLoad() {
			this.isLogin = this.$store.getters.isLogin()
			if (this.isLogin) {
				this.getUserInfo();
			} else {
				// #ifdef H5 || APP-PLUS
				toLogin();
				// #endif 
				// #ifdef MP
				this.isAuto = true;
				this.$set(this, 'isShowAuth', true)
				// #endif
				//this.$i18n.locale = 'zh-Hans'
				//uni.setStorageSync('lang', 'zh');
				if (uni.getLocale() == 'en') this.bzIndex = 0
				if (uni.getLocale() == 'zh-Hans') this.bzIndex = 1
				if (uni.getLocale() == 'zh-Hant') this.bzIndex = 2
				if (uni.getLocale() == 'ja') this.bzIndex = 3
				if (uni.getLocale() == 'th') this.bzIndex = 4
				if (uni.getLocale() == 'vi') this.bzIndex = 5
				if (uni.getLocale() == 'id') this.bzIndex = 6
				if (uni.getLocale() == 'hi') this.bzIndex = 7
				if (uni.getLocale() == 'tr') this.bzIndex = 8
				if (uni.getLocale() == 'ar') this.bzIndex = 9
				if (uni.getLocale() == 'ko') this.bzIndex = 10
			}
		},
		methods: {
			bindBzChange: function(e) {
				if (e.target.value == 0) {
					uni.setLocale('en');
					this.$i18n.locale = 'en'
				}
				if (e.target.value == 1) {
					uni.setLocale('zh-Hans');
					this.$i18n.locale = 'zh-Hans'
				}
				if (e.target.value == 2) {
					uni.setLocale('zh-Hant');
					this.$i18n.locale = 'zh-Hant'
				}
				if (e.target.value == 3) {
					uni.setLocale('ja');
					this.$i18n.locale = 'ja'
				}
				if (e.target.value == 4) {
					uni.setLocale('th');
					this.$i18n.locale = 'th'
				}
				if (e.target.value == 5) {
					uni.setLocale('vi');
					this.$i18n.locale = 'vi'
				}
				if (e.target.value == 6) {
					uni.setLocale('id');
					this.$i18n.locale = 'id'
				}
				if (e.target.value == 7) {
					uni.setLocale('hi');
					this.$i18n.locale = 'hi'
				}
				if (e.target.value == 8) {
					uni.setLocale('tr');
					this.$i18n.locale = 'tr'
				}
				if (e.target.value == 9) {
					uni.setLocale('ar');
					this.$i18n.locale = 'ar'
				}
				if (e.target.value == 10) {
					uni.setLocale('ko');
					this.$i18n.locale = 'ko'
				}
				this.bzArray = [{
					DEPARTNAME: this.$t('index.lang.en')
				}, {
					DEPARTNAME: this.$t('index.lang.zh-Hant')
				}, {
					DEPARTNAME: this.$t('index.lang.zh-Hans')
				}, {
					DEPARTNAME: this.$t('index.lang.ja')
				}, {
					DEPARTNAME: this.$t('index.lang.th')
				}, {
					DEPARTNAME: this.$t('index.lang.vi')
				}, {
					DEPARTNAME: this.$t('index.lang.id')
				}, {
					DEPARTNAME: this.$t('index.lang.hi')
				}, {
					DEPARTNAME: this.$t('index.lang.tr')
				}, {
					DEPARTNAME: this.$t('index.lang.ar')
				}, {
					DEPARTNAME: this.$t('index.lang.ko')
				}];
				//,{DEPARTNAME:this.$t('index.lang.vi')},{DEPARTNAME:this.$t('index.lang.id')},{DEPARTNAME:this.$t('index.lang.hi')},{DEPARTNAME:this.$t('index.lang.tr')},{DEPARTNAME:this.$t('index.lang.ar')}
				console.log('picker发送选择改变，携带值为', e.target.value)
				this.bzIndex = e.target.value;
			},
			/**
			 * 授权回调
			 */
			onLoadFun: function() {
				this.isShowAuth = false;
				this.getUserInfo();
			},
			// 授权關闭
			authColse: function(e) {
				this.isShowAuth = e
			},
			/**
			 * 小程序设置
			 */
			Setting: function() {
				uni.openSetting({
					success: function(res) {
						console.log(res.authSetting)
					}
				});
			},
			switchAccounts: function(item, index) {
				let userInfo = this.switchUserInfo[index],
					that = this;
				that.userIndex = index;
				if (that.switchUserInfo.length <= 1) return true;
				if (userInfo === undefined) return that.$util.Tips({
					title: this.$t('Notexit')
				});

				uni.showLoading({
					title: this.$t('changeIng')
				});
				switchH5Login({
					uid: item.uid
				}).then(({
					data
				}) => {
					uni.hideLoading();
					let newTime = Math.round(new Date() / 1000);
					that.$store.commit("LOGIN", {
						'token': data.token,
						'time': data.exp
					});
					that.$store.commit("SETUID", data.user.uid);
					that.$store.commit('UPDATE_USERINFO', data.user);
					that.getUserInfo();

				}).catch(err => {
					uni.hideLoading();
					return that.$util.Tips({
						title: err
					});
				})

			},
			/**
			 * 退出登錄
			 * 
			 */
			outLogin: function() {
				let that = this;
				if (that.loginType == 'h5') {
					uni.showModal({
						title: this.$t('index.tip'),
						content: this.$t('isOut'),
						cancelText: this.$t('cancel'), // 取消按钮
						confirmText: this.$t('ok'), // 确认按钮文
						success: function(res) {
							if (res.confirm) {
								getLogout()
									.then(res => {
										that.$store.commit("LOGOUT");
										uni.switchTab({
											url: '/pages/index/index',
										});
									})
									.catch(err => {
										console.log(err);
									});
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				}
			},
			/**
			 * 注销账号（假
			 * 
			 */
			outLogin2: function() {
				let that = this;
				if (that.loginType == 'h5') {
					uni.showModal({
						title: this.$t('index.tip2'),
						content: "清除缓存后需要重新登录",
						cancelText: this.$t('cancel2'), // 取消按钮
						confirmText: this.$t('ok2'), // 确认按钮文
						success: function(res) {
							if (res.confirm) {
								getLogout()
									.then(res => {
										that.$store.commit("LOGOUT");
										uni.switchTab({
											url: '/pages/index/index',
										});
									})
									.catch(err => {
										console.log(err);
									});
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				}
			},
			// 获取用户列表
			userAcc() {
				userAcc().then(res => {
					let that = this
					let switchUserInfo = res.data || [];
					for (let i = 0; i < switchUserInfo.length; i++) {
						if (switchUserInfo[i].uid == that.userInfo.uid) that.userIndex = i;
						// 切割h5用户；user_type状态：h5、routine（小程序）、wechat（公众號）；注：只有h5未注册手機號時，h5才可和小程序或是公众號数据想通；
						//#ifdef H5
						if (
							!that.$wechat.isWeixin() &&
							switchUserInfo[i].user_type != "h5" &&
							switchUserInfo[i].phone === ""
						)
							switchUserInfo.splice(i, 1);
						//#endif
					}
					that.$set(that, "switchUserInfo", switchUserInfo);
				})
			},
			editUsername() {
				uni.navigateTo({
					url: `/pages/users/user_name/index`
				})
			},
			/**修改密碼*/
			changePwd() {
				let that = this;
				if (that.userInfo.phone) {
					uni.navigateTo({
						url: `/pages/users/user_modify_pwd/index`
					})
				} else {
					that.$util.Tips({
						title: this.$t('fistBlind')
					});
				}
			},
			/**
			 * 获取用户详情
			 */
			getUserInfo: function() {
				let that = this;
				getUserInfo().then(res => {
					that.$set(that, 'userInfo', res.data);
					that.userAcc();
				});
			},
			/**
			 * 上傳文件
			 * 
			 */
			uploadpic: function() {
				let that = this;
				that.$util.uploadImageOne('upload/image', function(res) {
					let userInfo = that.switchUserInfo[that.userIndex];
					if (userInfo !== undefined) {
						userInfo.avatar = res.data.url;
					}
					that.switchUserInfo[that.userIndex] = userInfo;
					that.$set(that, 'switchUserInfo', that.switchUserInfo);
				});
			},

			/**
			 * 提交修改
			 */
			formSubmit: function(e) {
				let that = this,
					value = e.detail.value,
					userInfo = that.switchUserInfo[that.userIndex];
				if (!value.nickname) return that.$util.Tips({
					title: this.$t('emptyName')
				});
				value.avatar = userInfo.avatar;
				userEdit(value).then(res => {
					return that.$util.Tips({
						title: res.msg,
						icon: 'success'
					}, {
						tab: 3,
						url: 1
					});
				}).catch(msg => {
					return that.$util.Tips({
						title: msg || this.$t('pushFail')
					}, {
						tab: 3,
						url: 1
					});
				});
			}
		}
	}
</script>

<style scoped lang="scss">
	.personal-data .wrapper {
		margin: 10rpx 0;
		background-color: #fff;
		padding: 36rpx 30rpx 13rpx 30rpx;
	}

	.personal-data .wrapper .title {
		margin-bottom: 30rpx;
		font-size: 32rpx;
		color: #282828;
	}

	.personal-data .wrapper .wrapList .item {
		width: 690rpx;
		height: 160rpx;
		background-color: #f8f8f8;
		border-radius: 20rpx;
		margin-bottom: 22rpx;
		padding: 0 30rpx;
		position: relative;
		border: 2rpx solid #f8f8f8;
		box-sizing: border-box;
	}

	.personal-data .wrapper .wrapList .item.on {
		border-color: #e93323;
		border-radius: 20rpx;
		background-image: url("");
		background-size: 100% 100%;
		background-color: #fff9f9;
		background-repeat: no-repeat;
	}

	.personal-data .wrapper .wrapList .item .picTxt {
		width: 445rpx;
	}

	.personal-data .wrapper .wrapList .item .picTxt .pictrue {
		width: 96rpx;
		height: 96rpx;
		position: relative;
		border-radius: 15rpx;
		border: 1rpx solid #999;
		background-color: rgba(255, 255, 255, .8);
	}

	.personal-data .wrapper .wrapList .item .picTxt .pictrue image {
		width: 100%;
		height: 100%;
		border-radius: 50%;
	}

	.personal-data .wrapper .wrapList .item .picTxt .pictrue .alter {
		width: 30rpx;
		height: 30rpx;
		border-radius: 50%;
		position: absolute;
		bottom: 0;
		right: 0;
	}

	.personal-data .wrapper .wrapList .item .picTxt .text {
		width: 325rpx;
	}

	.personal-data .wrapper .wrapList .item .picTxt .text .name {
		width: 100%;
		font-size: 30rpx;
		color: #282828;
	}

	.personal-data .wrapper .wrapList .item .picTxt .text .phone {
		font-size: 24rpx;
		color: #999;
		margin-top: 10rpx;
	}

	.personal-data .wrapper .wrapList .item .bnt {
		font-size: 24rpx;
		background-color: #fff;
		border-radius: 27rpx;
		width: 140rpx;
		height: 54rpx;
		border: 2rpx solid #e93323;
	}

	.personal-data .wrapper .wrapList .item .currentBnt {
		position: absolute;
		right: 0;
		top: 0;
		font-size: 26rpx;
		background-color: rgba(233, 51, 35, 0.1);
		width: 140rpx;
		height: 48rpx;
		border-radius: 0 20rpx 0 20rpx;
	}

	.personal-data .list {
		margin-top: 15rpx;
		background-color: #fff;
	}

	.personal-data .list .item {
		padding: 30rpx 30rpx 30rpx 0;
		border-bottom: 1rpx solid #f2f2f2;
		margin-left: 30rpx;
		font-size: 32rpx;
		color: #282828;
	}

	.personal-data .list .item .phone {
		width: 160rpx;
		height: 56rpx;
		font-size: 24rpx;
		color: #fff;
		line-height: 56rpx;
		border-radius: 32rpx
	}

	.personal-data .list .item .pictrue {
		width: 88rpx;
		height: 88rpx;
	}

	.personal-data .list .item .pictrue image {
		width: 100%;
		height: 100%;
		border-radius: 50%;
	}

	.personal-data .list .item .input {
		width: 415rpx;
		text-align: right;
		color: #868686;
	}

	.personal-data .list .item .input .id {
		width: 365rpx;
	}

	.personal-data .list .item .input .iconfont {
		font-size: 35rpx;
	}

	.personal-data .modifyBnt {
		font-size: 32rpx;
		color: #fff;
		width: 690rpx;
		height: 90rpx;
		border-radius: 50rpx;
		text-align: center;
		line-height: 90rpx;
		margin: 76rpx auto 0 auto;
	}

	.personal-data .logOut {
		font-size: 32rpx;
		text-align: center;
		width: 690rpx;
		height: 90rpx;
		border-radius: 45rpx;
		margin: 30rpx auto 0 auto;
	}
</style>